package com.doctor.windflower_doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.AuthCodeAction;
import com.doctor.windflower_doctor.actionBeen.DoctorAction;

import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class Registration extends BaseActivity {
	private ImageButton back_imageButton;
	private EditText phone_emial_editText, password_editText,
			auth_code_editText;
	private ImageView show;
	private Button getcode_Button, register_Button;
	private TimeCount count;
	private int i = 1;
	private final String REGISTER_URL = "http://192.168.199.178:3000/api/doctor/register";
	private final String AUTHCODE_URL = "http://182.92.79.214:3002/api/user/create_send_code";
	private final String VERIFYAUTHCODE_URL = "http://182.92.79.214:3002/api/user/verify_send_code";
	private SharedPreferencesDB db;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		register_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!phone_emial_editText.getText().toString().trim()
						.equals("")
						&& !password_editText.getText().toString().trim()
								.equals("")
						&& !auth_code_editText.getText().toString().trim()
								.equals("")) {
					verifyAuthCode();

				} else if (phone_emial_editText.getText().toString().trim()
						.equals("")) {
					Toast.makeText(Registration.this, "手机号不能为空",
							Toast.LENGTH_SHORT).show();
				} else if (password_editText.getText().toString().trim()
						.equals("")) {
					Toast.makeText(Registration.this, "密码不能为空",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(Registration.this, "验证码不能为空",
							Toast.LENGTH_SHORT).show();
				}
			}
		});
		getcode_Button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!phone_emial_editText.getText().toString().trim()
						.equals("")) {
					getAuthCode();
					v.setBackgroundResource(R.drawable.wait_button);
					count.start();
				} else {
					Toast.makeText(Registration.this, "手机号不能为空",
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		back_imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		show.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				i++;
				if (i % 2 == 0) {
					show.setImageResource(R.drawable.show_password);
					password_editText
							.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				} else {
					show.setImageResource(R.drawable.unshow_password);
					password_editText.setInputType(InputType.TYPE_CLASS_TEXT
							| InputType.TYPE_TEXT_VARIATION_PASSWORD);
				}
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		db = SharedPreferencesDB.getInstance(Registration.this);
		getcode_Button = (Button) findViewById(R.id.getcode_Button);
		register_Button = (Button) findViewById(R.id.register_Button);
		count = new TimeCount(60000, 1000);
		phone_emial_editText = (EditText) findViewById(R.id.phone_emial_editText);
		password_editText = (EditText) findViewById(R.id.password_editText);
		auth_code_editText = (EditText) findViewById(R.id.auth_code_editText);
		back_imageButton = (ImageButton) findViewById(R.id.back_imageButton);
		show = (ImageView) findViewById(R.id.imageView_show);

	}

	private void verifyAuthCode() {
		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<AuthCodeAction> requestCallBack = new HttpRequestCallBack<AuthCodeAction>(
				new JsonParser(), AuthCodeAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<AuthCodeAction> responseInfo) {
				if (responseInfo.success.success) {
					register();
				} else {
					Toast.makeText(Registration.this,
							responseInfo.success.message, Toast.LENGTH_SHORT)
							.show();
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				Toast.makeText(Registration.this, msg, Toast.LENGTH_SHORT)
						.show();

			}

			@Override
			public void onStart() {

				super.onStart();
			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				VERIFYAUTHCODE_URL, requestCallBack);
		params.addBodyParameter("telephone", phone_emial_editText.getText()
				.toString().trim());
		params.addBodyParameter("appsecret", "123123");
		params.addBodyParameter("code", auth_code_editText.getText().toString()
				.trim());
		HttpRequest.send(params);

	}

	private void register() {
		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<DoctorAction> requestCallBack = new HttpRequestCallBack<DoctorAction>(
				new JsonParser(), DoctorAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<DoctorAction> responseInfo) {
				if (responseInfo.success.success) {
					db.setIsLogin(true);
					db.setDocotorId(responseInfo.success.data.accountId);
					db.setUserName(phone_emial_editText.getText().toString()
							.trim());
					startActivity(new Intent(Registration.this,
							DoctorAuth.class));
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				Toast.makeText(Registration.this, msg, Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onStart() {

				super.onStart();
			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				REGISTER_URL, requestCallBack);
		params.addBodyParameter("username", phone_emial_editText.getText()
				.toString().trim());
		params.addBodyParameter("password", password_editText.getText()
				.toString().trim());
		params.addBodyParameter("authcode", auth_code_editText.getText()
				.toString().trim());
		HttpRequest.send(params);

	}

	private void getAuthCode() {
		HttpMethod requestMethod = HttpMethod.POST;
		HttpRequestCallBack<AuthCodeAction> requestCallBack = new HttpRequestCallBack<AuthCodeAction>(
				new JsonParser(), AuthCodeAction.class) {
			@Override
			protected void handleResult(
					HttpResponseInfo<AuthCodeAction> responseInfo) {
				if (responseInfo.success.success) {
					Toast.makeText(Registration.this, "发送验证码才成功！！",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(Registration.this,
							responseInfo.success.message, Toast.LENGTH_SHORT)
							.show();
				}

			}

			@Override
			protected void handleError(HttpException e, String msg) {
				Toast.makeText(Registration.this, msg, Toast.LENGTH_SHORT)
						.show();
			}

			@Override
			public void onStart() {
				super.onStart();

			}

		};
		HttpRequestParams params = new HttpRequestParams(requestMethod,
				AUTHCODE_URL, requestCallBack);
		params.addBodyParameter("telephone", phone_emial_editText.getText()
				.toString().trim());
		params.addBodyParameter("appsecret", "123123");
		HttpRequest.send(params);

	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.registration;
	}

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			getcode_Button.setText("重新验证");
			getcode_Button.setClickable(true);
			getcode_Button.setBackgroundResource(R.drawable.auth_button);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			getcode_Button.setClickable(false);
			getcode_Button.setText(millisUntilFinished / 1000 + "");
		}
	}

}
